package com.springbootproduct.feignservice.impl;

import com.codingapi.txlcn.tc.annotation.DTXPropagation;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.springbootproduct.entity.Product;
import com.springbootproduct.feignservice.ProductFeignService;
import com.springbootproduct.mapper.ProductMapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/product")
public class ProductFeignServiceImpl implements ProductFeignService {

    @Resource
    private ProductMapper productMapper;

    @Override
    public List<Product> listData(Product product) {
        return productMapper.findAll(product);
    }

    //事物的参与方
    @LcnTransaction(propagation = DTXPropagation.SUPPORTS)
    @Transactional
    @Override
    public int updateByProduct(Product product){
        try {
            return  productMapper.updateByPrimaryKeySelective(product);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    @Transactional
    @Override
    public int save(Product product){
        try {
            return productMapper.insertSelective(product);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    public  Product getOne(String id){
      return   productMapper.selectByPrimaryKey(id);
    }
}
